package com.zzk.lanouhn.myapplication1.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.BaseAdapter;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.InvestPinglunAuthorPhotoDaAdapter;
import com.zzk.lanouhn.myapplication1.modles.InvestPingLunAuthorPhotoModle;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/19.
 */
public class InvestpinglunAuthorPhotoDa extends AppCompatActivity {

    private RecyclerView recyclerView;
    //适配器
    private InvestPinglunAuthorPhotoDaAdapter daAdapter;
    //实体类
    private InvestPingLunAuthorPhotoModle modle;
    //接受从别的页面穿过来的ID
    private SharedPreferences sp;
    private String DatuId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //布局里就一个RecyclerView（和listView一样）
        setContentView(R.layout.activity_invest_pinglun_author_photo_da);
        recyclerView = (RecyclerView) findViewById(R.id.author_photo_da_recycleView);

        sp = getSharedPreferences("photo", MODE_PRIVATE);
        DatuId = sp.getString("photodatu", "12345678");

        //LinearLayoutManager是线性
        LinearLayoutManager layoutManager = new LinearLayoutManager(InvestpinglunAuthorPhotoDa.this);
        //滑动方向：HORIZONTAL和VERTICAL
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);

        //GridLayoutManager是瀑布流，你可以看看效果
//        GridLayoutManager glm = new GridLayoutManager(InvestpinglunAuthorPhotoDa.this, 2);
//        glm.setOrientation(LinearLayoutManager.HORIZONTAL);
//        recyclerView.setLayoutManager(glm);

        getPhotoDaData();
    }

    private void getPhotoDaData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //接口
                String url = "http://user.zhangyoubao.com/apis/rest/UsersService/getOtherUserPhotos?" +
                        "other_userid=" + DatuId + "&userid=19316729&token=780662dcfb78c86be20ed34abd0e4e20de4&i_=" +
                        "K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=&deviceId=" +
                        "K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=&t_=1474273521163&p_=5834&v_=" +
                        "400902&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=22&cha=baiduMartket&u_=19316729&modle";
                //打开网络
                OkHttpClient client = new OkHttpClient();
                Request requst = new Request.Builder().url(url).build();
                Call call = client.newCall(requst);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //打开网络失败
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //得到从网上获取的数据
                        String ss = response.body().string();
                        //使用第三方解析
                        Gson gson = new Gson();
                        modle = gson.fromJson(ss, InvestPingLunAuthorPhotoModle.class);
                        //判断是否解析完成
                        if (modle.getData().size() > 0) {
                            //成功（正在解析）
                            handler.sendEmptyMessage(1);
                        } else {
                            //没有可解析的了
                            handler.sendEmptyMessage(2);
                        }
                    }
                });
            }
        }).start();
    }
    //Handler你应该知道怎么回事
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    //使适配器，数据和ecyclerView绑定
                    daAdapter = new InvestPinglunAuthorPhotoDaAdapter(InvestpinglunAuthorPhotoDa.this, modle);
                    recyclerView.setAdapter(daAdapter);
                    break;
                case 2:
                    break;
            }
        }
    };

}
